#!/bin/sh
#
# Copyright (C) 2000-2025 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# Test the events code with filters
#
TestName="events-filter-test"
JobName=backup
. scripts/functions

scripts/cleanup
scripts/copy-confs

#
# Zap out any schedule in default conf file so that
#  it doesn't start during our test
#
outf="$tmp/sed_tmp"
echo "s%  Schedule =%# Schedule =%g" >${outf}
cp $scripts/bacula-dir.conf $tmp/1
sed -f ${outf} $tmp/1 >$scripts/bacula-dir.conf

$bperl -e "add_attribute('$conf/bacula-dir.conf', 'catalog', 'all,events,!events.connection,!events.bweb', 'Messages')"
$bperl -e "add_attribute('$conf/bacula-dir.conf', 'append', '$tmp/bweb.log = events.bweb', 'Messages')"
$bperl -e "add_attribute('$conf/bacula-dir.conf', 'console', 'all,!skipped,!saved,events', 'Messages')"
$bperl -e "add_attribute('$conf/bacula-dir.conf', 'append', '$tmp/bweb.log = events.bweb', 'Messages')"
$bperl -e "add_attribute('$conf/bacula-dir.conf', 'file', '$tmp/connection.log = events.connection,!events.bweb', 'Messages')"

change_jobname BackupClient1 $JobName
start_test

cat <<END_OF_DATA >$tmp/bconcmds
@$out $tmp/log1.out
run job=$JobName level=full yes
wait
run job=$JobName level=full yes
wait
messages
delete jobid=1
purge volume=Vol-0001 yes
run job=$JobName level=full yes
cancel jobid=3 yes
wait
delete volume=Vol-0001 yes
.events type=bweb source=eric text="User login"
.events type=bweb source=eric text="User logout"
.events type="*error*" source=eric text="error!!"
quit
END_OF_DATA

run_bacula
stop_bacula

cat <<END_OF_DATA >$tmp/bconcmds
@$out $tmp/log2.out
list events
@$out $tmp/log3.out
llist events
quit
END_OF_DATA

run_bacula
stop_bacula

grep "User login" $tmp/log2.out > /dev/null
if [ $? = 0 ]; then
    print_debug "ERROR: should not find bweb user login in $tmp/log2.out"
    estat=1
fi

grep "User logout" $tmp/log2.out > /dev/null
if [ $? = 0 ]; then
    print_debug "ERROR: should not find bweb user logout in $tmp/log2.out"
    estat=1
fi

grep "User login" $tmp/bweb.log > /dev/null
if [ $? != 0 ]; then
    print_debug "ERROR: should find bweb user login in $tmp/bweb.log"
    estat=1
fi

grep "User logout" $tmp/bweb.log > /dev/null
if [ $? != 0 ]; then
    print_debug "ERROR: should find bweb user logout in $tmp/bweb.log"
    estat=1
fi

grep "error" $tmp/log2.out > /dev/null
if [ $? = 0 ]; then
    print_debug "ERROR: should not find incorrect message in $tmp/log2.out"
    estat=1
fi

grep "Director startup" $tmp/log2.out > /dev/null
if [ $? != 0 ]; then
    print_debug "ERROR: should find Director startup message in $tmp/log2.out"
    estat=1
fi

grep "Director shutdown" $tmp/log2.out > /dev/null
if [ $? != 0 ]; then
    print_debug "ERROR: should find Director shutdown message in $tmp/log2.out"
    estat=1
fi

grep "Connection" $tmp/log2.out > /dev/null
if [ $? = 0 ]; then
    print_debug "ERROR: should not find Console connection message in $tmp/log2.out"
    estat=1
fi

grep "delete volume" $tmp/log2.out > /dev/null
if [ $? != 0 ]; then
    print_debug "ERROR: should find delete volume message in $tmp/log2.out"
    estat=1
fi

grep "purge jobid" $tmp/log2.out > /dev/null
if [ $? != 0 ]; then
    print_debug "ERROR: should find delete volume message in $tmp/log2.out"
    estat=1
fi

$bperl -e 'check_events_json("Daemon", "Console", "Events")'
if [ $? != 0 ]; then
    print_debug "ERROR: should find Events in Daemon/Console in  bdirjson output"
    estat=1
fi

$bperl -e 'check_events_json("Daemon", "Catalog", "!Events.bweb")'
if [ $? != 0 ]; then
    print_debug "ERROR: should find Events.bweb in Daemon/Catalog in  bdirjson output"
    estat=1
fi

$bperl -e 'check_events_json("Standard", "Catalog", "!Events.bweb")'
if [ $? != 0 ]; then
    print_debug "ERROR: should find Events.bweb in Standard/Catalog in  bdirjson output"
    estat=1
fi

end_test
